package com.meida.student.app.scholartyrant.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.mybatis.interceptor.PageInterceptor;
import com.meida.common.mybatis.model.PageParams;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.module.bus.client.entity.BusOrderAppraise;
import com.meida.module.bus.provider.service.BusOrderAppraiseService;
import com.meida.module.user.client.entity.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("SOrderAppraisePageListHandler")
public class SOrderAppraisePageListHandler implements PageInterceptor<BusOrderAppraise> {

    @Autowired
    private BusOrderAppraiseService busOrderAppraiseService;

    @Override
    public void prepare(CriteriaQuery<BusOrderAppraise> cq, PageParams pageParams, EntityMap params) {
        cq.clear();
        cq.select(BusOrderAppraise.class, "appraiseContent", "score", "createTime");
        cq.select(AppUser.class, "nickName", "userName", "avatar");
        cq.eq(BusOrderAppraise.class, "answerUserId");
        cq.createJoin(AppUser.class).setMainField("appraiseUserId").setJoinField("userId");
    }

    @Override
    public void complete(CriteriaQuery<BusOrderAppraise> cq, List<EntityMap> result, EntityMap extra) {
        Long answerUserId = cq.getLong("answerUserId");
        Long totalAppraiseNum = busOrderAppraiseService.countTotalAppraiseNum(answerUserId);
        extra.put("totalAppraiseNum", totalAppraiseNum);
        //todo 评论时间格式化规则
    }

}
